package service

import (
	"context"
	"fmt"
	"os"

	pb "demo/api"
	"demo/internal/dao"
	"demo/internal/rpc"
	"git.huoys.com/middle-end/kratos/pkg/conf/paladin"

	types "github.com/gogo/protobuf/types"
	"github.com/google/wire"
)

var Provider = wire.NewSet(New, wire.Bind(new(pb.DemoServer), new(*Service)))

// Service service.
type Service struct {
	ac  *paladin.Map
	dao dao.Dao
	rpc *rpc.Client
}

// New new a service and return.
func New(d dao.Dao, r *rpc.Client) (s *Service, cf func(), err error) {
	s = &Service{
		ac:  &paladin.TOML{},
		dao: d,
		rpc: r,
	}
	cf = s.Close
	//err = paladin.Watch("application.txt", s.ac)
	return
}

// SayHello grpc demo func.
func (s *Service) SayHello(ctx context.Context, req *pb.HelloReq) (reply *pb.HelloResp, err error) {
	hostname, err := os.Hostname()
	reply = &pb.HelloResp{
		Content: "hostname : " + hostname,
	}
	return
}

// SayHelloURL bm demo func.
func (s *Service) SayHelloURL(ctx context.Context, req *pb.HelloReq) (reply *pb.HelloResp, err error) {
	hostname, err := os.Hostname()
	reply = &pb.HelloResp{
		Content: "hostname : " + hostname,
	}
	fmt.Printf("hello url ")
	return
}

// Ping ping the resource.
func (s *Service) Ping(ctx context.Context, e *types.Empty) (*types.Empty, error) {
	return &types.Empty{}, s.dao.Ping(ctx)
}

// Close close the resource.
func (s *Service) Close() {
}
